//
//  ZMMessageView.m
//  基本框架
//
//  Created by runone02 on 2018/7/23.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ZMMessageView.h"
#import <ReactiveCocoa.h>
#import "ZMDatePickerView.h"
#import "JXAlertview.h"
#import "CustomYearDatePicker.h"
#import "CustomDayDatePicker.h"
#import "TransitionViewController.h"
#import "CacheViewController.h"

#import "NormalListController.h"
//广告界面
#import "ADWKWebViewController.h"
#import "ADAlertView.h"
#import "ADModel.h"

//电子书类型
#import "TReaderViewController.h"

#import "RollingViewController.h"

#import "ZZGestureLockViewController.h"
@interface ZMMessageView ()<ZMDatePickerViewDelegate,CustomAlertDelegete,ADAlertDelegate>
/** */
@property (nonatomic, strong) ZMDatePickerView *pickerView;
/**年选择器*/
@property (nonatomic, strong) CustomYearDatePicker *yearDpicker;
/** 标记类型*/
@property (nonatomic, assign) NSInteger mark;
/**标记选中日期 */
@property (nonatomic, strong) NSString *selectDate;
/** */
@property (nonatomic, strong) CustomDayDatePicker *dayDpicker;
//广告图片数组
@property (nonatomic, strong) NSMutableArray *imgArr;
@end


@implementation ZMMessageView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self loadArr];
    }
    return self;
}

- (void)setupUI {
    self.zeroBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.zeroBtn.titleLabel.font = TITLE_FONT(30);
    [self.zeroBtn setTitle:@"按钮0" forState:UIControlStateNormal];
    [self.zeroBtn addTarget:self action:@selector(showPickViewerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.zeroBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.zeroBtn.layer.masksToBounds = YES;
    self.zeroBtn.layer.borderWidth = 1.0f;
    self.zeroBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.zeroBtn.layer.cornerRadius = W_RATIO(10);
    
    [self addSubview:self.zeroBtn];
    [self.zeroBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(kTopBarHeight);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];

    self.zeroLabel = [[UILabel alloc]init];
    self.zeroLabel.textColor = [UIColor blackColor];
    self.zeroLabel.text = @"文字0";
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nameLabelClick)];
    [self.zeroLabel addGestureRecognizer:labelTapGestureRecognizer];
    self.zeroLabel.userInteractionEnabled = YES; // 可以理解为设置label可被点击
    self.zeroLabel.font = TITLE_FONT(30);
    self.zeroLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.zeroLabel];
    [self.zeroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.zeroBtn.mas_bottom).with.offset(10);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    self.oneBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.oneBtn.titleLabel.font = TITLE_FONT(30);
    [self.oneBtn setTitle:@"按钮1" forState:UIControlStateNormal];
    [self.oneBtn addTarget:self action:@selector(yearBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.oneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.oneBtn.layer.masksToBounds = YES;
    self.oneBtn.layer.borderWidth = 1.0f;
    self.oneBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.oneBtn.layer.cornerRadius = W_RATIO(10);
    
    [self addSubview:self.oneBtn];
    [self.oneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.zeroLabel.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    
    self.twoBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.twoBtn.titleLabel.font = TITLE_FONT(30);
    [self.twoBtn setTitle:@"按钮2" forState:UIControlStateNormal];
    [self.twoBtn addTarget:self action:@selector(dayBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.twoBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.twoBtn.layer.masksToBounds = YES;
    self.twoBtn.layer.borderWidth = 1.0f;
    self.twoBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.twoBtn.layer.cornerRadius = W_RATIO(10);

    [self addSubview:self.twoBtn];
    [self.twoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneBtn.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    self.testBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.testBtn.titleLabel.font = TITLE_FONT(30);
    [self.testBtn setTitle:@"缓存数据" forState:UIControlStateNormal];
    
    [self.testBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.testBtn.layer.masksToBounds = YES;
    self.testBtn.layer.borderWidth = 1.0f;
    self.testBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.testBtn.layer.cornerRadius = W_RATIO(10);
    [self.testBtn addTarget:self action:@selector(tsetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.testBtn];
    
    [self.testBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.twoBtn.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];

    self.navigationBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.navigationBtn.titleLabel.font = TITLE_FONT(30);
    [self.navigationBtn setTitle:@"导航栏" forState:UIControlStateNormal];
    
    [self.navigationBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.navigationBtn.layer.masksToBounds = YES;
    self.navigationBtn.layer.borderWidth = 1.0f;
    self.navigationBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.navigationBtn.layer.cornerRadius = W_RATIO(10);
    [self.navigationBtn addTarget:self action:@selector(navigationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.navigationBtn];
    
    [self.navigationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.testBtn.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    self.UpshiftHeadBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    self.UpshiftHeadBtn.titleLabel.font = TITLE_FONT(30);
    [self.UpshiftHeadBtn setTitle:@"弹出广告" forState:UIControlStateNormal];
    
    [self.UpshiftHeadBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.UpshiftHeadBtn.layer.masksToBounds = YES;
    self.UpshiftHeadBtn.layer.borderWidth = 1.0f;
    self.UpshiftHeadBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.UpshiftHeadBtn.layer.cornerRadius = W_RATIO(10);
    [self.UpshiftHeadBtn addTarget:self action:@selector(UpshiftHeadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.UpshiftHeadBtn];
    
    [self.UpshiftHeadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navigationBtn.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    
    UIButton *fanye = [UIButton buttonWithType: UIButtonTypeCustom];
    fanye.titleLabel.font = TITLE_FONT(30);
    [fanye setTitle:@"翻页类型电子书" forState:UIControlStateNormal];
    
    [fanye setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    fanye.layer.masksToBounds = YES;
    fanye.layer.borderWidth = 1.0f;
    fanye.layer.borderColor = [UIColor lightGrayColor].CGColor;
    fanye.layer.cornerRadius = W_RATIO(10);
    [fanye addTarget:self action:@selector(fanyeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:fanye];
    
    [fanye mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.UpshiftHeadBtn.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    
    
    UIButton *hudong = [UIButton buttonWithType: UIButtonTypeCustom];
    hudong.titleLabel.font = TITLE_FONT(30);
    [hudong setTitle:@"滑动类型电子书" forState:UIControlStateNormal];
    
    [hudong setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    hudong.layer.masksToBounds = YES;
    hudong.layer.borderWidth = 1.0f;
    hudong.layer.borderColor = [UIColor lightGrayColor].CGColor;
    hudong.layer.cornerRadius = W_RATIO(10);
    [hudong addTarget:self action:@selector(hudongBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:hudong];
    
    [hudong mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(fanye.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    UIButton *yaw = [UIButton buttonWithType: UIButtonTypeCustom];
    yaw.titleLabel.font = TITLE_FONT(30);
    [yaw setTitle:@"滚动页面" forState:UIControlStateNormal];
    
    [yaw setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    yaw.layer.masksToBounds = YES;
    yaw.layer.borderWidth = 1.0f;
    yaw.layer.borderColor = [UIColor lightGrayColor].CGColor;
    yaw.layer.cornerRadius = W_RATIO(10);
    [yaw addTarget:self action:@selector(yawBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:yaw];
    
    [yaw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hudong.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
    
    UIButton *jiesuo = [UIButton buttonWithType: UIButtonTypeCustom];
    jiesuo.titleLabel.font = TITLE_FONT(30);
    [jiesuo setTitle:@"解锁界面" forState:UIControlStateNormal];
    
    [jiesuo setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    jiesuo.layer.masksToBounds = YES;
    jiesuo.layer.borderWidth = 1.0f;
    jiesuo.layer.borderColor = [UIColor lightGrayColor].CGColor;
    jiesuo.layer.cornerRadius = W_RATIO(10);
    [jiesuo addTarget:self action:@selector(jiesuoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:jiesuo];
    
    [jiesuo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(yaw.mas_bottom).with.offset(10);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
    }];
      self.yearDpicker = [[CustomYearDatePicker alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 250)];
    
     self.dayDpicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 250)];
    
    
}

- (void)navigationBtnClick:(UIButton *)btn{
    NormalListController *sec = [[NormalListController alloc]init];
    [self.viewController.navigationController pushViewController:sec animated:YES];
}


- (void)showPickViewerAction {
    self.pickerView = [[ZMDatePickerView alloc] initDatePickerWithDefaultDate:nil
                                                                         andDatePickerMode:UIDatePickerModeDate];
    
    self.pickerView.delegate = self;
    [self.pickerView show];
}


- (void)nameLabelClick{
    [self.viewController presentViewController:[[TransitionViewController alloc]init] animated:YES completion:^{
        
    }];
}

//返回时间
- (void)pickerView:(ZMDatePickerView *)pickerView didSelectDateString:(NSString *)dateString {
    self.zeroBtn.titleLabel.text = dateString;
}

- (void)yearBtn:(UIButton *)sender{
    JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 300)];

    alert.center = self.center;
    alert.delegate = self;
    [alert initwithtitle:@"标题" andcommitbtn:@"确定" andStr:@"取消"];
    [alert addSubview:self.yearDpicker];
    [alert show];
    _mark = 3;
}

- (void)dayBtn:(UIButton *)dender{
    JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, 300)];

    alert.center = self.center;
    alert.delegate = self;
    [alert initwithtitle:@"选择" andcommitbtn:@"确定" andStr:@"取消"];
    [alert addSubview:self.dayDpicker];
    [alert show];
    _mark = 1;
}

- (void)btnindex:(int)index :(int)tag {
    if (index == 2 ) {
        if (_mark == 1) {
//       _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.dayDpicker.year,self.dayDpicker.month,self.dayDpicker.day];
            _selectDate = [NSString stringWithFormat:@"%d-%@-%@",self.dayDpicker.year,self.dayDpicker.month,self.dayDpicker.day];
            
             [self.twoBtn setTitle:_selectDate forState:UIControlStateNormal];
            
        }else if (_mark == 2){

        }else{
            _selectDate = [NSString stringWithFormat:@"%d",self.yearDpicker.year];
             [self.oneBtn setTitle:_selectDate forState:UIControlStateNormal];
        }
    }
}

- (void)tsetBtnClick:(UIButton *)btn{
    CacheViewController * cache = [[CacheViewController alloc]init];
    [self.viewController.navigationController pushViewController:cache animated:YES];
}


//弹出广告页面
- (void)UpshiftHeadBtnClick:(UIButton *)btn{
 [ADAlertView showInView:self theDelegate:self theADInfo:_imgArr placeHolderImage:@"1"];
}

//广告图片数组
- (void)loadArr{
    NSMutableArray *arr = [[NSMutableArray alloc]initWithCapacity:0];
    for (int i = 1; i<= 5; i++) {
        ADModel *adModel  = [[ADModel alloc]init];
        adModel.imgStr = [NSString stringWithFormat:@"%d.jpg",i];
       
        [arr addObject:adModel];
    }
    _imgArr = arr;
}


//广告点击的图片
- (void)clickAlertViewAtIndex:(NSInteger)index{
    ADModel *adModel = [_imgArr objectAtIndex:index];
    if (index == 0) {
        adModel.linkUrl = @"http://code.cocoachina.com/list/20/5";
    }else if (index == 1){
        adModel.linkUrl = @"http://code.cocoachina.com/view/133854";
    }else if (index == 2){
         adModel.linkUrl = @"https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/";
    }else if (index == 3){
         adModel.linkUrl = @"https://blog.csdn.net/dongzhensong/article/details/54343951";
    }else if (index == 4){
         adModel.linkUrl = @"https://blog.csdn.net/xxw888/article/details/73640139";
    }else{
        adModel.linkUrl = @"https://www.baidu.com";
    }
    ADWKWebViewController *webVC = [[ADWKWebViewController alloc]init];
    webVC.urlStr = adModel.linkUrl;
    [self.viewController.navigationController pushViewController:webVC animated:YES];
}



#pragma mark  翻页电子书
- (void)fanyeBtnClick:(UIButton *)btn{
    TReaderViewController *vc = [[TReaderViewController alloc]init];
    vc.style = TReaderTransitionStylePageCur;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark   滑动电子书
- (void)hudongBtnClick:(UIButton *)btn{
    TReaderViewController *vc = [[TReaderViewController alloc]init];
    vc.style = TReaderTransitionStyleScroll;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)yawBtnClick:(UIButton *)btn{
    RollingViewController *ro = [[RollingViewController alloc]init];
    [self.viewController.navigationController pushViewController:ro animated:YES];
}

- (void)jiesuoBtnClick:(UIButton *)btn{
    ZZGestureLockViewController * ges = [[ZZGestureLockViewController alloc]init];
    [self.viewController.navigationController pushViewController:ges animated:YES];
}
@end
